package cn.edu.xjtu.carlWay.tree.sumofLeftLeaves;

import cn.edu.xjtu.Util.TreeNode.TreeNode;

/**
 * 404. 左叶子之和
 * 计算给定二叉树的所有左叶子之和。
 * <p>
 * https://leetcode-cn.com/problems/sum-of-left-leaves/
 */
public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return sum(root.left, true) + sum(root.right, false);
    }

    public int sum(TreeNode node, boolean isLeft) {
        if (node == null) {
            return 0;
        }
        if (isLeft && node.left == null && node.right == null) {
            return node.val;
        }
        return sum(node.left, true) + sum(node.right, false);
    }
}
